package com.yay.warehouse.tabs;

import java.util.List;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.yay.warehouse.Contact;
import com.yay.warehouse.IKeys;
import com.yay.warehouse.R;
import com.yay.warehouse.Step3;
import com.yay.warehouse.model.Category;
import com.yay.warehouse.model.LFType;
import com.yay.warehouse.model.Ticket;
import com.yay.warehouse.service.L10N;
import com.yay.warehouse.service.TicketsService;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class LFMap extends MapActivity {
	private MapView mapView;
	private List<Overlay> mapOverlays;
	private Drawable drawable;
	private LostItemizedOverlay itemizedOverlay;
	private MapController mc;
	private TicketsService ticketsService;
	private Intent step3;
	private Button btnNext;
	private Intent contact;
	
	
	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        initMap();
	    initBtnNext();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(TicketsAdapter.reload) {
			initMap();
		}
	}


	// PRIVATE METHODS
	
	private TicketsService getTicketsService() {
		if(ticketsService == null) {
			ticketsService = new TicketsService();
		}
		return ticketsService;
	}

	private Intent getStep3() {
		if(step3 == null) {
			step3 = new Intent(LFMap.this, Step3.class); 
		}
		return step3;
	}
	
	private void initBtnNext() {
		if(btnNext == null) {
			btnNext = (Button) findViewById(R.id.btnNext);
		    btnNext.setTypeface(L10N.font);
	        btnNext.setOnClickListener(new OnClickListener() {
		    	 @Override
		    	 public void onClick(View arg0) {
		    		 startActivity(getStep3());
		    		 finish();
		    	 }
		    });
		}
	}
	
	private Intent getContact() {
		if(contact == null) {
			contact = new Intent(LFMap.this, Contact.class); 
		}
		return contact;
	}

	private void initMap() {
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        
        Ticket currentTicket = TicketsService.getCurrentTicket();
        GeoPoint point = new GeoPoint((int)(currentTicket.getLatitude()), (int)(currentTicket.getLongitude()));
        
        if(TicketsAdapter.getTickets().size() == 0 || TicketsAdapter.reload) {
        	TicketsAdapter.getTickets().clear();
        	TicketsAdapter.reload = false;
        	int type = currentTicket.getType() == LFType.LOST.getId() ? LFType.FOUND.getId() : LFType.LOST.getId();
        	TicketsAdapter.getTickets().addAll(getTicketsService().find(type, currentTicket.getCategory(), currentTicket.getLatitude(), currentTicket.getLongitude()));
        }

        GeoPoint tPoint;
        for(final Ticket ticket: TicketsAdapter.getTickets()) {
        	tPoint = new GeoPoint((int)(ticket.getLatitude()), (int)(ticket.getLongitude()));
        	OverlayItem overlayitem = new OverlayItem(tPoint, "", "");
        	drawable = this.getResources().getDrawable(Category.findById(ticket.getCategory()).getDrawable());
        	itemizedOverlay = new LostItemizedOverlay(drawable) {
        		@Override
        		protected boolean onTap(int index) {
        			getContact().putExtra(IKeys.TICKET, ticket);
            		startActivity(getContact());
        			return true;
        		}
        	};
        	itemizedOverlay.addOverlay(overlayitem);
        	mapOverlays.add(itemizedOverlay);
        }
        
        mc = mapView.getController();
        mc.animateTo(point);
        mc.setZoom(17); 
        mapView.invalidate();
	}
	
}
